import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:homemedia/model/enum/MediaType.dart';
import 'package:homemedia/model/media.dart';
import 'package:homemedia/page/route_config.dart';
import 'package:homemedia/page/widget/loading_widget.dart';
import 'package:homemedia/util/Constant.dart';
import 'package:text_scroll/text_scroll.dart';
import 'package:uuid/uuid.dart';

class VideoItemCard extends StatelessWidget {
  final MediaLibraryItem entry;

  VideoItemCard({
    super.key,
    required this.entry,
  });

  static const uuid = Uuid();
  final String imgTag = uuid.v4();
  final String titleTag = uuid.v4();
  final String authorTag = uuid.v4();

  @override
  Widget build(BuildContext context) {

    return Focus(
      onKeyEvent: (node, event) {
        return KeyEventResult.ignored;
      },
      child: Builder(
        builder: (context) {
          FocusNode node = Focus.of(context);
          return SizedBox(
            width: 230,
            height: 400,
            child: Column(
              children: [
                SizedBox(
                  width: 230,
                  height: 350,
                  child: Card(
                    shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(10.0)),
                    ),
                    elevation: node.hasFocus ? 5.0:4.0,
                    shadowColor: node.hasFocus ? const Color.fromARGB(255, 85, 210, 255) : null,
                    child: InkWell(
                      borderRadius: const BorderRadius.all(Radius.circular(10.0)),
                      onTap: () {
                        MediaType mediaType = MediaType.getMediaByType(entry.type!);
                        switch (mediaType) {
                          case MediaType.Movies:
                            VideoInfo videoInfo = VideoInfo();
                            videoInfo.name = entry.name;
                            videoInfo.uuid = entry.uuid;
                            videoInfo.type = entry.type;
                            Get.toNamed(RouteConfig.player, arguments: videoInfo);
                          case MediaType.Tvshows:

                          case MediaType.Mixed:
                          // TODO: Handle this case.
                          case MediaType.Music:
                          // TODO: Handle this case.
                          case MediaType.Photo:
                          // TODO: Handle this case.
                        }
                      },
                      child: ClipRRect(
                        borderRadius: const BorderRadius.all(
                          Radius.circular(10.0),
                        ),
                        child: Hero(
                          tag: imgTag,
                          child: CachedNetworkImage(
                            imageUrl: "${Constant.apihost}/v1/media/library/item/${entry.uuid}/image/poster",
                            placeholder: (context, url) => const LoadingWidget(
                              isImage: true,
                            ),
                            errorWidget: (context, url, error) => Image.asset(
                              'assets/images/place.png',
                              fit: BoxFit.cover,
                            ),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                entry.name!.length > 10 ?
                    TextScroll(
                      entry.name!,
                      intervalSpaces: 10,
                      style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                      velocity: Velocity(pixelsPerSecond: Offset(50, 0)),
                    )
                    :
                    Text(entry.name!, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
                Text("2022")
              ],
            )
            
            
          );
        },
      )
    );
  }
}
